package ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.border.CompoundBorder;

import layout.TableLayout;

public class GalleryMainOrganizer extends JPanel {
	private static final long serialVersionUID = 1L;

	private JPanel content;	
	private RenderingHints hints;	
	private JPanel panel;
	private JScrollPane scrollPanel;

	private Dimension screen = new Dimension(java.awt.Toolkit
			.getDefaultToolkit().getScreenSize().width, java.awt.Toolkit
			.getDefaultToolkit().getScreenSize().height);
	private double[][] tbSize = new double[][] { { TableLayout.FILL },
			{ 0.05, TableLayout.FILL } };	
	

	private GalleryOrganizer organizer;

	public GalleryOrganizer getMainOrganizer() {
		return organizer;
	}



	public void setOrganizer(GalleryOrganizer organizer) {
		this.organizer = organizer;
	}



	private LinkedList<GalleryMainItem> mainItems;

	public GalleryMainOrganizer(GalleryOrganizer organizer) {
		this.organizer = organizer;
		this.mainItems = organizer.getMainItems();
		for (GalleryMainItem item : mainItems) {
			item.setOrganizer(this);
		}
		HashMap<Key, Object> map = new HashMap<Key, Object>();
		map.put(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		map.put(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);

		map.put(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		hints = new RenderingHints(map);

		content = new JPanel();
		content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
		content.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
		content.setOpaque(false);
		this.scrollPanel = new JScrollPane(content);
//		scrollPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 250, 50));
		scrollPanel.setBorder(new CompoundBorder(BorderFactory.createEmptyBorder(20, 50, 250, 50),BorderFactory.createEtchedBorder()));	
		this.setLayout(new TableLayout(tbSize));
		this.panel = new JPanel();
		this.panel.setLayout(new GridLayout(1, 1));
		this.panel.add(scrollPanel);
		this.add(panel, "0,1");
		this.setPreferredSize(screen);
		content.setOpaque(false);
		scrollPanel.setOpaque(false);
		scrollPanel.setDoubleBuffered(true);
		scrollPanel.getVerticalScrollBar().setUnitIncrement(15);
		panel.setOpaque(false);
		this.setOpaque(false);
		scrollPanel.getViewport().setOpaque(false);
		for (GalleryMainItem item2 : mainItems) {
			content.add(item2);
		}
	}	
	
	

	public void delete(GalleryMainItem item) {
		mainItems.remove(item);
		content.removeAll();
		for (GalleryMainItem item2 : mainItems) {
			content.add(item2);
		}
		this.revalidate();
		this.repaint();
	}

	public JPanel getContent() {
		return content;
	}
	

	public RenderingHints getHints() {
		return hints;
	}

	
	public boolean isFirst(GalleryMainItem item) {
		return mainItems.indexOf(item) == 0;
	}

	public boolean isLast(GalleryMainItem item) {
		return mainItems.indexOf(item) == mainItems.size() - 1;
	}

	public void moveDown(GalleryMainItem item) {
		item.getButtons()[3].setEnabled(true);
		int vorher = mainItems.indexOf(item);
		if (vorher != mainItems.size() - 1) {
			mainItems.remove(item);
			mainItems.add(vorher + 1, item);
			content.removeAll();
			for (GalleryMainItem item2 : mainItems) {
				content.add(item2);
			}
			this.revalidate();
			this.repaint();
		}
		JScrollBar vsb = scrollPanel.getVerticalScrollBar();
		if (mainItems.indexOf(item) > 1) {			
			vsb.setValue(vsb.getValue() + (vsb.getVisibleAmount() / 7));
		}
		if (mainItems.indexOf(item) == mainItems.size() - 1)
			item.getButtons()[4].setEnabled(false);
	}

	public void moveUp(GalleryMainItem item) {
		item.getButtons()[4].setEnabled(true);
		int vorher = mainItems.indexOf(item);
		if (vorher != 0) {
			mainItems.remove(item);
			mainItems.add(vorher - 1, item);
			content.removeAll();
			for (GalleryMainItem item2 : mainItems) {
				content.add(item2);
			}
			this.revalidate();
			this.repaint();
		}
		JScrollBar vsb = scrollPanel.getVerticalScrollBar();
		vsb.setValue(vsb.getValue() - (vsb.getVisibleAmount() / 7));
		if (mainItems.indexOf(item) == 0)
			item.getButtons()[3].setEnabled(false);
	}

	

	public LinkedList<GalleryMainItem> getMainItems() {
		return this.mainItems;
	}
	

}